<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleleaf.org">
<head th:insert="base">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" action="" method="post">
            <div class="layui-form-item">
                <label for="permissionName" class="layui-form-label">
                    权限名称
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="permissionName" name="permissionName" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="permissionCode" class="layui-form-label">
                    权限代码
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="permissionCode" name="permissionCode" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>权限的唯一标识
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    权限类型
                </label>
                <div class="layui-input-inline">
                    <select id="permissionType" name="permissionType" class="valid" lay-filter="permissionType">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item" id="parentCodeDiv">
                <label for="parentCode" class="layui-form-label">
                    父权限代码
                </label>
                <div class="layui-input-inline">
                    <select id="parentCode" name="parentCode" class="valid">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    权限类型为菜单必填
                </div>
            </div>
            <div class="layui-form-item" id="showIconDiv">
                <label for="showIcon" class="layui-form-label">
                    图标
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="showIcon" name="showIcon" lay-filter="showIcon"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="orderNum" class="layui-form-label">
                    排序
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="orderNum" name="orderNum" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" id = "urlDiv">
                <label for="permissionUrl" class="layui-form-label">
                    url
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="permissionUrl" name="permissionUrl"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    权限类型为菜单必填
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <button  class="layui-btn" lay-filter="add" lay-submit="">
                    确认新增
                </button>
            </div>
        </form>
    </div>
</div>
<script th:inline="none">
    layui.use(['form','jquery','enums','iconPicker','tool','permissionUtil'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var enums = layui.enums;
        var iconPicker = layui.iconPicker;
        var permissionUtil = layui.permissionUtil;
        var tool = layui.tool;
        var baseUrl = "/back/permission";

        init_form();

        /**
         * 初始化页面
         */
        function init_form() {
            enums.createSelectByEnum('permissionType','EnumPermissionType',null,null,'请选择权限类型');
            form.render();//重新渲染
        }

        iconPicker.render({
            // 选择器，推荐使用input
            elem: '#showIcon',
            // 数据类型：fontClass/unicode，推荐使用fontClass
            type: 'fontClass',
            // 是否开启搜索：true/false
            search: true,
            // 是否开启分页
            page: false,
            // 每页显示数量
            limit: 12
        });

        form.on('select(permissionType)', function(data){
            var permissionType = data.value;
            if("0" == permissionType){
                //选中菜单组
                $("#parentCodeDiv").hide();
                $("#parentCode").attr("lay-verify","");
                $("#showIconDiv").show();
                $("#showIcon").attr("lay-verify","required");
                $("#urlDiv").hide();
                $("#url").attr("lay-verify","");
            }
            if("1" == permissionType){
                //选中菜单
                $("#parentCodeDiv").show();
                $("#showIconDiv").hide();
                $("#showIcon").attr("lay-verify","");
                $("#parentCode").attr("lay-verify","required");
                $("#urlDiv").show();
                $("#url").attr("lay-verify","required");
                //获取已存在菜单组的数据，生成选择框
                permissionUtil.createParentCodeSelect("parentCode","0",false,null,'请选择父权限代码');
                form.render();
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            var res = tool.submit(baseUrl + '/add',data.field);
            if(res.code == return_200){
                layer.msg(res.msg, {
                        icon: msg.success,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }else {
                layer.msg(res.msg, {
                        icon: msg.error,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }
            return false;
        });
    });
</script>
</body>
</html>